package com.yunshipei.core.ui.client;

import android.net.http.SslError;
import android.webkit.ValueCallback;

import com.yunshipei.core.utils.NetUtils;

import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkView;

class BaseXWalkViewClient extends XWalkResourceClient {

    BaseXWalkViewClient(XWalkView view) {
        super(view);
    }

    @Override
    public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
        switch (description) {
            case "net::ERR_ADDRESS_UNREACHABLE":
                description = "无法连接到服务器";
                break;
            case "net::ERR_CONNECTION_TIMED_OUT":
                description = "网络请求超时";
                break;
            case "net::ERR_NAME_NOT_RESOLVED":
                if (!NetUtils.isNetworkAvailable(view.getContext())) {
                    description = "网络不可用，请检查网络";
                    break;
                }
                description = "域名解析失败，请检查服务器";
                break;
            case "net::ERR_FILE_NOT_FOUND":
                description = "文件不存在";
                break;
            case "net::ERR_UNKNOWN_URL_SCHEME":
                description = "网页载入失败";
                break;
            case "net::ERR_PROXY_CONNECTION_FAILED":
                description = "代理连接失败";
                break;
            case "net::ERR_EMPTY_RESPONSE":
                description = "服务器已断开连接，且未发送任何数据";
                break;
            case "net::ERR_CACHE_READ_FAILURE":
                description = "缓存读取失败";
                break;
            default:
                break;
        }
        super.onReceivedLoadError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onReceivedSslError(XWalkView view, ValueCallback<Boolean> callback, SslError error) {
        callback.onReceiveValue(true);
    }
}
